/**
 * 详情对话框
 */
var BasicLinePriceInfoDlg = {
    data: {
        priceName: "",
        priceMode: "",
        price: "",
        lineId: "",
        lineName: "",
        deptId: "",
        deliverId: "",
        note: "",
        createTimes: "",
        createName: "",
        v1: "",
        v2: "",
        v3: "",
        v4: ""
    }
};
/**
 * 添加或者修改页面
 */
var BasicCarrierInfoDlg = {
    data: {
        carrierId: "",
        carrierName: "",
    }
};

layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/basicLinePrice/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('basicLinePriceForm', result.data);

    if (result.data.priceType == '固定') {
        $('#priceCsxx').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
        $('#priceCssx').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
        $('#priceCsxx').val("");
        $('#priceCssx').val("");
    } else {
        $('#priceCsxx').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
        $('#priceCssx').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
    }
    if (result.data.priceMode != '元/点') {
        $('#priceQb').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
        $('#priceQbds').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
        $('#prceQbcd').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
        $('#priceQb').val("");
        $('#priceQbds').val("");
        $('#prceQbcd').val("");
    } else {
        $('#priceQb').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
        $('#priceQbds').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
        $('#prceQbcd').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
    }

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/basicLinePrice/editItem", function (data) {
            Feng.success("更新成功！");
        	var Index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(Index);
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    form.on('radio(priceType)', function (data) {
        var selectedValue = data.value;
        if (selectedValue == '固定') {
            $('#priceCsxx').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
            $('#priceCssx').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
            $('#priceCsxx').val("");
            $('#priceCssx').val("");
        } else {
            $('#priceCsxx').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
            $('#priceCssx').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
        }
    });

    form.on('radio(priceMode)', function (data) {
        var selectedValue2 = data.value;
        if (selectedValue2 !== '元/点') {
            $('#priceQb').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
            $('#priceQbds').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
            $('#prceQbcd').attr('readonly', true).css('background-color', '#f0f0f0').removeAttr('lay-verify');
            $('#priceQb').val("");
            $('#priceQbds').val("");
            $('#prceQbcd').val("");
        } else {
            $('#priceQb').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
            $('#priceQbds').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
            $('#prceQbcd').attr('readonly', false).css('background-color', '#fff').attr('lay-verify', 'required');
        }
    });
    
    //点击承运商
    $("#carrierName").click(function(){
        layer.open({
            type: 2,
            title: '选择承运商',
            area: ['900px', '550px'],
            content: Feng.ctxPath + '/basicLinePrice/toCarrierName',
            end: function () {
                $("#carrierId").val(BasicCarrierInfoDlg.data.carrierId);
                $("#carrierName").val(BasicCarrierInfoDlg.data.carrierName);
            }
        });
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});